import axios from 'axios';
import Config from 'config';

import Web3Manager from '../../libs/web3-manager';
import AuthenticationManager from '../authentication/authentication-manager';

export default class PaymentManager {
    static setTransactionHash = async (
        disbursementID: number,
        transactionHash: string,
        currency: string,
        network: string
    ): Promise<boolean> => {
        const data = {
            disbursement_id: disbursementID,
            transaction_hash: transactionHash,
            token: currency,
            network,
        };

        const headers = {
            [AuthenticationManager.KEY]: AuthenticationManager.getKey(),
            [AuthenticationManager.SECRET]: AuthenticationManager.getSecret(),
            wallet_address: await Web3Manager.getWalletAddress(),
        };

        const call = await axios.post(
            Config.getAPI() + '/multi-chain-payment/set-transaction-hash',
            data,
            {
                headers,
            }
        );

        if (call.data?.error) {
            throw call.data.error;
        }

        return call.data && call.data['payment'] && call.data['payment']['id'] == disbursementID;
    };
}
